package com.webank.wedatasphere.linkis.metadata.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author xxj
 */
public class JdbcUtils {


    public static Connection getDatabaseMetaData(String dbType, String jdbcUsername, String jdbcPassword, String jdbcUrl) {
        String driver = null;


        if (jdbcUrl.contains(Constants.MYSQL)) {
            driver = Constants.MYSQLJDBC;
        } else if (jdbcUrl.contains(Constants.POSTGRESQL)) {
            driver = Constants.POSTGRESQLJDBC;
        } else if (jdbcUrl.contains(Constants.ORACLE)) {
            driver = Constants.ORACLEJDBC;
        } else if (jdbcUrl.contains(Constants.GREENPLUM)) {
            driver = Constants.GREENPLUMJDBC;
        }
        try {
            //1、注册驱动
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2、获取连接
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;


    }

}